﻿#include "RenderEngine.h"

#include <QDebug>

namespace GeneralUI {

	RenderEngine::RenderEngine()
	{

	}

	RenderEngine::~RenderEngine()
	{

	}

	void RenderEngine::drawLineShadow(QPainter *painter, QPointF p1, QPointF p2, int scale)
	{
		painter->save();

		painter->setPen(QPen(QColor(0, 228, 255), 1));
		QColor color(0, 0, 0, 50);
		int shadowWidth = int(6 * qSqrt(scale));
		int step = int(180 / qSqrt(shadowWidth));

		for (int i = 0; i < shadowWidth; i++)
		{
			color.setAlpha(180 - int(qSqrt(i) * step));
			painter->setPen(color);

			if ((p1.rx() < p2.rx() && p1.ry() < p2.ry()) ||
				(p1.rx() > p2.rx() && p1.ry() > p2.ry()))
			{
				painter->drawLine(QPointF(p1.rx() + i, p1.ry() - i), QPointF(p2.rx() + i, p2.ry() - i));
			}
			else
			{
				painter->drawLine(QPointF(p1.rx() + i, p1.ry() + i), QPointF(p2.rx() + i, p2.ry() + i));
			}
		}

		painter->restore();
	}

	void RenderEngine::drawTextShadow(QPainter *painter, QPointF p, QString text, int scale)
	{
		painter->save();

		QColor color(0, 0, 0, 50);
		int shadowWidth = int(9 * qSqrt(scale));
		int step = int(90 / qSqrt(shadowWidth));

		for (int i = 0; i < shadowWidth; i++)
		{
			color.setAlpha(90 - int(qSqrt(i) * step));
			painter->setPen(color);

			painter->drawText(QPointF(p.rx() + i / 2.0, p.ry() + i / 3.0), text);
		}

		painter->restore();
	}

	void RenderEngine::drawTextShadow(QPainter *painter, QRectF rect, QString text, int scale)
	{
		painter->save();

		QColor color(0, 0, 0, 50);
		int shadowWidth = int(9 * qSqrt(scale));
		int step = int(90 / qSqrt(shadowWidth));

		for (int i = 0; i < shadowWidth; i++)
		{
			color.setAlpha(90 - int(qSqrt(i) * step));
			painter->setPen(color);

			QRectF newRect = QRectF(rect.x() + i / 2.0, rect.y() + i / 3.0, rect.width(), rect.height());
			painter->drawText(newRect, Qt::AlignLeft | Qt::AlignVCenter, text);
		}

		painter->restore();
	}

	void RenderEngine::drawPathShadow(QPainter *painter, QPainterPath path, int scale)
	{
		painter->save();

		QColor color(0, 0, 0, 100);
		painter->setPen(QPen(color, 1));

		int shadowWidth = int(4 * qSqrt(scale));
		int opacity = 80;
		int step = int(opacity / qSqrt(shadowWidth));

		for (int i = 0; i < shadowWidth; i++)
		{
			color.setAlpha(opacity - int(qSqrt(i) * step));
			painter->setPen(color);
			QPainterPath p = path.translated(i, i);           // 右下偏移
			painter->drawPath(p);
		}

		painter->restore();
	}

	void RenderEngine::drawPathLineShadow(QPainter *painter, QPainterPath path, int scale)
	{
		painter->save();

		QColor color(0, 0, 0, 100);

		int shadowWidth = int(4 * qSqrt(scale));
		int opacity = 150;
		int step = int(opacity / qSqrt(shadowWidth));

		for (int i = 0; i < shadowWidth; i++)
		{
			color.setAlpha(opacity - int(qSqrt(i) * step));
			painter->setPen(color);
			QPainterPath p = path.translated(i, i);           // 右下偏移
			painter->drawPath(p);
		}

		painter->restore();
	}

	void RenderEngine::drawPathRectShadow(QPainter *painter, QPainterPath path, int scale)
	{
		painter->save();

		painter->setPen(QPen(QColor(0, 228, 255), 5));
		QColor color(0, 0, 0, 100);
		int shadowWidth = 5 * scale;
		int opacity = 50;
		int step = int(opacity / qSqrt(shadowWidth));

		for (int i = 0; i < shadowWidth; i++)
		{
			color.setAlpha(int(qSqrt(i) * step));
			painter->setPen(color);

			QPainterPathStroker stroker;
			stroker.setWidth(shadowWidth - i);
			QPainterPath p = stroker.createStroke(path);
			painter->drawPath(p);
		}

		painter->restore();
	}

}//namespace OfUI 
